﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;

namespace Project5188.BUS.EducationAndTraining
{
    public class TimeTables : DetailView<timetable>
    {
        private project5188Entities _context;
        // ReSharper disable InconsistentNaming
        private new List<timetable> EntityList;
        private new timetable Entity;
        // ReSharper restore InconsistentNaming

        public long TransactionId { get; private set; }

        #region Overrides of DetailView<timetable>

        public override List<timetable> GetEntityList()
        {
            _context = new project5188Entities();
            EntityList = _context.timetables.Where(a => !a.IsDeleted).OrderBy(a => a.a_ID).ToList();

            return EntityList;
        }

        public override List<timetable> GetEntityById(long id)
        {
            _context = new project5188Entities();
            EntityList = _context.timetables.Where(a => a.a_ID == id && !a.IsDeleted).OrderBy(a => a.a_ID).ToList();

            return EntityList;
        }

        public override timetable GetEntity()
        {
            _context = new project5188Entities();
            Entity = new timetable();
            return Entity;
        }

        public override void Update()
        {
            _context.SaveChanges();
        }

        public override void Insert()
        {
            _context.AddTotimetables(Entity);
            _context.SaveChanges();
            TransactionId = Entity.a_ID;
        }

        #endregion

        public List<timetable> GetEntityListByParameters(string courseName, string courseCode, string teacherName, string coursePeriod, DateTime? givenDate)
        {
            _context = new project5188Entities();

            var parameterList = new List<ObjectParameter>();

            var dynamicString = "it.IsDeleted = @IsDeleted";
            parameterList.Add(new ObjectParameter("IsDeleted", false));

            dynamicString += " And it.course.IsDeleted = @CourseIsDeleted";
            parameterList.Add(new ObjectParameter("CourseIsDeleted", false));

            dynamicString += " And it.course.teacherregistry.IsDeleted = @TeacherIsDeleted";
            parameterList.Add(new ObjectParameter("TeacherIsDeleted", false));

            if (!string.IsNullOrEmpty(courseName) && !string.IsNullOrWhiteSpace(courseName))
            {
                dynamicString += " And it.course.c_CourseName = @CourseName";
                parameterList.Add(new ObjectParameter("CourseName", courseName));
            }

            if (!string.IsNullOrEmpty(courseCode) && !string.IsNullOrWhiteSpace(courseCode))
            {
                dynamicString += " And it.course.e_CourseCode = @CourseCode";
                parameterList.Add(new ObjectParameter("CourseCode", courseCode));
            }

            if (!string.IsNullOrEmpty(teacherName) && !string.IsNullOrWhiteSpace(teacherName))
            {
                dynamicString += " And it.course.teacherregistry.d_NameSurname = @TeacherName";
                parameterList.Add(new ObjectParameter("TeacherName", teacherName));
            }

            if (!string.IsNullOrEmpty(coursePeriod) && !string.IsNullOrWhiteSpace(coursePeriod))
            {
                dynamicString += " And it.d_CoursesPeriod = @CoursePeriod";
                parameterList.Add(new ObjectParameter("CoursePeriod", coursePeriod));
            }

            if (givenDate != null)
            {
                dynamicString += " And it.c_GivenDate = @GivenDate";
                parameterList.Add(new ObjectParameter("GivenDate", givenDate));
            }

            EntityList = _context.timetables.Where(dynamicString, parameterList.ToArray()).OrderBy(a => a.a_ID).ToList();

            return EntityList;
        }
    }
}
